package com.brillilab.service.core.lab.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.entity.PageVo;
import com.brillilab.dao.mapper.lab.LabUserCardMapper;
import com.brillilab.domain.enums.lab.LabUserCardEnum;
import com.brillilab.domain.pa.lab.LabUserCardPa;
import com.brillilab.domain.po.lab.LabUserCard;
import com.brillilab.domain.vo.lab.LabMemberCardVo;
import com.brillilab.domain.vo.lab.LabUserCardVo;
import com.brillilab.service.core.lab.ILabUserCardService;
import com.github.pagehelper.PageHelper;

import javax.annotation.Resource;


@Service
@Transactional
public class LabUserCardServiceImpl implements ILabUserCardService {

	@Resource
	private LabUserCardMapper labUserCardMapper;
	
	@Override
	public LabUserCard empowerCard(Long labId, String cardNo, String certigier) {	
		LabUserCard userCard = this.selectCard(labId, cardNo);
		if(userCard==null){
			userCard = new LabUserCard();
			userCard.setLabId(labId);
			userCard.setCardNo(cardNo);
			userCard.setCertigier(certigier);
			userCard.setState(LabUserCardEnum.State.AUTHORIZED.getValue());
			labUserCardMapper.insert(userCard);
		}
		return userCard;
	}

	@Override
	public LabUserCard bindCard(Long labId, String cardNo, Long userId) {
		LabUserCard userCard = this.selectCard(labId, cardNo);
		if(userCard == null){
			LabUserCard card = new LabUserCard();
			card.setLabId(labId);
			card.setCardNo(cardNo);
			card.setUserId(userId);
			card.setBindingTime(new Date());
			card.setState(LabUserCardEnum.State.TIED_CARD.getValue());
			labUserCardMapper.insert(card);
			return card;
		}else {
			userCard.setUserId(userId);
			userCard.setBindingTime(new Date());
			userCard.setState(LabUserCardEnum.State.TIED_CARD.getValue());
			labUserCardMapper.updateById(userCard);
			return userCard;
		}
	}

	@Override
	public LabUserCard unbindCard(Long labId, String cardNo, Long userId) {
		LabUserCard userCard = this.selectCard(labId, cardNo);
		Assert.isTrue(userCard!=null && userId.equals(userCard.getUserId()), "无绑定信息");

		int b = labUserCardMapper.deleteById(userCard);
		Assert.isTrue(b==1, "解绑失败！");
		return userCard;
	}

	@Override
	public LabUserCard forceBindCard(Long labId,String cardNo,Long userId) {
		Assert.isTrue(labId!=null && StringUtils.isNotBlank(cardNo) && userId!=null,"参数缺失！");
		LabUserCard card=this.selectCard(labId,cardNo);
		card.setState(LabUserCardEnum.State.TIED_CARD.getValue());
		card.setUserId(userId);
		card.setBindingTime(new Date());
		card.setLabId(labId);
		Assert.isTrue(labUserCardMapper.updateById(card) == 1,"绑定失败！");
		return card;
	}

	@Override
	public boolean resetCard(Long labId,String cardNo) {
		Assert.isTrue(labId!=null && StringUtils.isNotBlank(cardNo),"参数缺失！");
		LabUserCard card=this.selectCard(labId,cardNo);
		card.setState(LabUserCardEnum.State.AUTHORIZED.getValue());
		card.setUserId(null);
		card.setBindingTime(null);
		card.setLabId(labId);
		return labUserCardMapper.resetCard(card) ==1;
	}

	@Override
	public LabUserCard selectCard(Long labId, String cardNo) {
	    Assert.isTrue(labId!=null && StringUtils.isNotBlank(cardNo),"参数缺失！");
		LambdaQueryWrapper<LabUserCard> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(LabUserCard::getLabId, labId);
		wrapper.eq(LabUserCard::getCardNo, cardNo);
		return labUserCardMapper.selectOne(wrapper);
	}

	@Override
	public PageVo<LabUserCardVo> selectPage(LabUserCardPa cardPa) {
		PageHelper.startPage(cardPa.getPageNum(), cardPa.getPageSize());
		List<LabUserCardVo> list = labUserCardMapper.selectPage(cardPa);
		return new PageVo<LabUserCardVo>(list);
	}

    @Override
    public LabUserCard selectCard(String cardNo) {
        Assert.isTrue(StringUtils.isNotBlank(cardNo),"参数缺失！");
        return labUserCardMapper.selectOne(
        		new LambdaQueryWrapper<LabUserCard>()
						.eq(LabUserCard::getCardNo,cardNo));
    }

	@Override
	public List<LabMemberCardVo> memberCardList(Long labId) {
		return labUserCardMapper.memberCardList(labId);
	}

    @Override
    public List<LabUserCard> selectListByLabIdInUserIds(Long labId,List<Long> userIds) {
		Assert.notNull(labId,"labId can not be null!");
		Assert.isTrue(userIds!=null && userIds.size()>0 ,"userIds can not be null or empty!");
        return labUserCardMapper.selectList(
        		new LambdaQueryWrapper<LabUserCard>()
						.eq(LabUserCard::getLabId,labId)
						.in(LabUserCard::getUserId,userIds));
    }

	@Override
	public LabUserCard selectListByLabIdAndUserId(Long labId,Long userId) {
		return labUserCardMapper.selectOne(
				new LambdaQueryWrapper<LabUserCard>()
				.eq(LabUserCard::getLabId,labId)
				.eq(LabUserCard::getUserId,userId));
	}

}
